Imports System.Data

Partial Class Admin_Add_CommRate
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.SetPageAccess(Session("UserName"))
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If broker_cmb.Items.Count < 1 Then
      ViewState.Clear()

      Dim zClass As New LookUp.General_Lookup
      broker_cmb.Items.Clear()
      zClass.Loader(broker_cmb, "BROKER", "broker_desc", "broker_cd")
      broker_cmb.Items.Insert(0, "")

      'zClass.LookUpCode = "COMMIS_RATE"
      'zClass.DataGridFill(DataGrid, Results_lbl)
      DataGrid.Visible = False
      Results_lbl.Text = "0"

    End If

    Dim Edit_Image As Image = CType(Me.Master.FindControl("C1"), Image)
    Edit_Image.ImageUrl = "~/images/redT.PNG"
    Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("CommissionRate_btn"), LinkButton)
    Edit_Button.ForeColor = Drawing.Color.Black
    Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
    Edit_Panel.Visible = True
    SecurePage()
  End Sub

  Protected Sub broker_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles broker_cmb.SelectedIndexChanged
    If broker_cmb.SelectedItem.Text = "" Then Exit Sub

    Dim zClass As New LookUp.General_Lookup
    provider_cmb.Items.Clear()
    zClass.Loader(provider_cmb, "PROVIDER", "provider_desc", "provider_cd", broker_cmb.SelectedValue)
    provider_cmb.Items.Insert(0, "")

    product_cmb.Items.Clear()
    DataGrid.Visible = False
    Results_lbl.Text = "0"

  End Sub

  Protected Sub provider_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles provider_cmb.SelectedIndexChanged
    If provider_cmb.SelectedItem.Text = "" Then Exit Sub

    Dim zClass As New LookUp.General_Lookup
    product_cmb.Items.Clear()
    zClass.Loader(product_cmb, "PRODUCT", "prdt_desc", "prdt_cd", provider_cmb.SelectedValue)
    product_cmb.Items.Insert(0, "")

    DataGrid.Visible = False
    Results_lbl.Text = "0"
  End Sub

  Protected Sub product_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles product_cmb.SelectedIndexChanged
    If product_cmb.SelectedItem.Text = "" Then Exit Sub

        DataGrid.CurrentPageIndex = 0
    DataGrid.Visible = True
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "COMMIS_RATE"
    zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)

  End Sub

  Private Sub OnMode()
    broker_cmb.Enabled = False
    provider_cmb.Enabled = False
    product_cmb.Enabled = False
  End Sub

  Private Sub NotOnMode()
    broker_cmb.Enabled = True
    provider_cmb.Enabled = True
    product_cmb.Enabled = True
  End Sub

  Protected Sub DataGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.CancelCommand
    DataGrid.EditItemIndex = -1
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "COMMIS_RATE"
    zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    ViewState.Remove("lookup_seq")
    NotOnMode()
  End Sub

  Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
    ViewState("lookup_seq") = e.Item.Cells(3).Text
    DataGrid.EditItemIndex = e.Item.ItemIndex
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "COMMIS_RATE"
    zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    OnMode()
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        DataGrid.EditItemIndex = -1
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "COMMIS_RATE"
        DataGrid.CurrentPageIndex = e.NewPageIndex
        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
        ViewState.Remove("lookup_seq")
        NotOnMode()
    End Sub

  Protected Sub DataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.UpdateCommand
    Dim commrate_txt As TextBox = e.Item.Cells(1).FindControl("TextBox1")
    Dim rate As String = commrate_txt.Text
    Dim zClass As New LookUp.General_Lookup

    If e.Item.Cells(4).Text = "&nbsp;" Then
      zClass.ExecQuery("INSERT INTO user_commission_rate_mstr(prdt_cd, advisor_level_cd, user_com_rate, " & _
          "user_com_crea_by, user_com_crea_dt) VALUES(" & _
          "'" & Trim(product_cmb.SelectedValue.ToString) & "'," & _
          "'" & ViewState("lookup_seq") & "'," & rate & "," & _
          "'" & UCase(Session("UserName")) & "','" & Now & "')")
    Else
      zClass.ExecQuery("UPDATE user_commission_rate_mstr SET " & _
        "user_com_rate=" & rate & ", " & _
        "user_com_last_upd_by='" & UCase(Session("UserName")) & "', " & _
        "user_com_last_upd_dt='" & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & "' " & _
        "WHERE (advisor_level_cd='" & ViewState("lookup_seq") & "') AND (prdt_cd=N'" & _
        Trim(product_cmb.SelectedValue.ToString) & "')")

    End If

    zClass.LookUpCode = "COMMIS_RATE"
    DataGrid.EditItemIndex = -1
    zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    NotOnMode()
    ViewState.Remove("lookup_seq")

  End Sub

End Class